﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lucid.ESA.ClientDataSystem.Application.Contracts;
using StructureMap;
using Lucid.ESA.ClientDataSystem.Application.DataTransferObjects;
using Lucid.ESA.ClientDataSystem.Application.Responses;

namespace Lucid.ESA.ClientDataSystem.UI.Controllers
{
    public class ClientController : Controller
    {
        public ActionResult Index()
        {
            IClientService clientService = ObjectFactory.GetInstance<IClientService>();
            GetResponse<List<ClientDto>> response = clientService.GetAllClients() as GetResponse<List<ClientDto>>;
            return View(response.Value);
        }

        public ActionResult Details(long id)
        {
            IClientService clientService = ObjectFactory.GetInstance<IClientService>();
            GetResponse<ClientDto> response = clientService.GetClientById(id) as GetResponse<ClientDto>;
            return View(response.Value);
        }

        public ActionResult Edit(long id)
        {
            IClientService clientService = ObjectFactory.GetInstance<IClientService>();
            GetResponse<ClientDto> response = clientService.GetClientById(id) as GetResponse<ClientDto>;
            return View(response.Value);
        }

        [HttpPost]
        public ActionResult Edit(FormCollection formCollection)
        {
            IClientService clientService = ObjectFactory.GetInstance<IClientService>();
            Response response = clientService.EditClient(Convert.ToInt64(formCollection["Id"]), formCollection["Name"]);

            if (response.Status == ResponseStatus.Success)
            {
                return RedirectToAction("/Index");
            }
            else
            {
                ValidationErrorResponse validationErrorResponse = response as ValidationErrorResponse;
                foreach (ValidationErrorResponseMessage error in validationErrorResponse.Errors)
                    ViewData.ModelState.AddModelError(error.Field == null ? "" : error.Field, error.Text);

                GetResponse<ClientDto> getResponse = clientService.GetClientById(Convert.ToInt64(formCollection["Id"])) as GetResponse<ClientDto>;
                return View(getResponse.Value);
            }
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ClientDto client)
        {
            IClientService clientService = ObjectFactory.GetInstance<IClientService>();
            Response response = clientService.CreateClient(client.Name);

            if (response.Status == ResponseStatus.Success)
            {
                return RedirectToAction("/Index");
            }
            else
            {
                ValidationErrorResponse validationErrorResponse = response as ValidationErrorResponse;
                foreach (ValidationErrorResponseMessage error in validationErrorResponse.Errors)
                    ViewData.ModelState.AddModelError(error.Field == null ? "" : error.Field, error.Text);

                return View(client);
            }
        }
    }
}
